package org.alvin;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class SoftCopyRightUtils {

    public static void main(String[] args) {
        String[] dir = {
//                "D:\\tangzhichao\\workspace\\xiaolan\\xl_platform_h5\\xblue_msite_v1\\src\\views\\auth",
//                "D:\\tangzhichao\\workspace\\xiaolan\\xl_platform_h5\\xblue_msite_v1\\src\\views\\layouts",
//                "D:\\tangzhichao\\workspace\\version\\xiaolandev\\clientcharge\\xiaolan_app_v3\\pages\\bike",
//                "D:\\tangzhichao\\workspace\\xiaolan\\xl_platform_h5\\xblue_msite_v1\\src\\views\\report",
//                "D:\\tangzhichao\\workspace\\xiaolan\\xl_platform_h5\\xblue_msite_v1\\src\\views\\main"
                "D:\\tangzhichao\\workspace\\dev_invoice_20231107\\electrombile\\xiaolancharging\\centerserver\\src\\main\\java\\com\\xl\\centerserver\\common\\handler"
        };
        String[] suffix = {".java", ".vue", "js", "ts"};
        String targetFile = "D:/copyRight1.txt";
        try (OutputStream out = Files.newOutputStream(Paths.get(targetFile))) {
            for (String dirName : dir) {
                appendTextByDir(Paths.get(dirName), out, suffix);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void appendTextByDir(Path filePath, OutputStream out, String[] suffix) throws IOException {
        System.out.println("文件：" + filePath);
        if (Files.isDirectory(filePath)) {
            Files.list(filePath).forEach(item -> {
                try {
                    appendTextByDir(item, out, suffix);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            });
            return;
        }
        boolean filter = false;
        for (String s : suffix) {
            filter = filePath.toString().endsWith(s);
            if (filter) {
                break;
            }
        }
        if (!filter) {
            System.out.println("过滤掉");
            return;
        }

//        byte[] buffs = Files.readAllBytes(filePath);
        out.write("\n".getBytes());
//        out.write(buffs);
        List<String> lines = Files.readAllLines(filePath);
        for (String line : lines) {
            if (line.trim().isEmpty()) {
                continue;
            }
            out.write(line.concat("\n").getBytes());
        }
        out.flush();
    }
}
